package com.css.dzj.dzjap.modules.administration.punish.controller;

import com.alibaba.fastjson.JSON;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.audit.service.SysAuditService;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.administration.check.entity.ApJgxwCheckAction;
import com.css.dzj.dzjap.modules.administration.punish.entity.Punish;
import com.css.dzj.dzjap.modules.administration.punish.service.PunishService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import static com.css.dzj.dzjap.modules.administration.check.entity.QApJgxwCheckAction.apJgxwCheckAction;

@RestController
@RequestMapping("/punish")
public class PunishController {

    //注入Service
    @Autowired
    private PunishService punishService;
    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    private HttpServletRequest request;

    //查询行政处罚行为信息表
    @GetMapping(value = "/getPunish")
    public R getPunish(@RequestParam("id") String id){
        String s;
        try {
            Punish punish = punishService.getPunish(id);
            s = JSON.toJSONString(punish);
            sysAuditService.saveSuccessAudit("行政处罚行为", Constant.PUNISH,Constant.PT,Constant.SELETE,s,request);
            return R.ok().put("data",punish);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(id);
            sysAuditService.saveFailAudit("行政处罚行为",Constant.PUNISH,Constant.PT,Constant.SELETE,s,e,request);
            return R.error("操作失败");
        }

    }


    // 添加行政处罚行为信息表
    @PostMapping(value = "/addPunish")
    public R addPunish(@RequestBody Punish push) {
        String s;
        try{
            String flag = punishService.addPunish(push);
            if (!"0".equals(flag)) {
                R r = new R();
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);
                s = JSON.toJSONString(push);
                sysAuditService.saveSuccessAudit("行政处罚行为", Constant.PUNISH,Constant.PT,Constant.ADD,s,request);
                return r;
            } else {
                return R.error("添加失败");
            }
        } catch (Exception e){
            s = JSON.toJSONString(push);
            sysAuditService.saveFailAudit("行政处罚行为",Constant.PUNISH,Constant.PT,Constant.ADD,s,e,request);
            return R.error(e.getMessage());
        }
    }

    // 修改行政处罚行为信息表
    @PostMapping(value = "/updatePunish")
    public R updatePunish(@RequestBody Punish push ){
        String s;
        try {
            Punish aPush = punishService.getPunish(push.getId());
            BeanUtil.copyBeanNotNullToBean(push, aPush);
            punishService.update(aPush);
            s = JSON.toJSONString(push);
            sysAuditService.saveSuccessAudit("行政处罚行为", Constant.PUNISH,Constant.PT,Constant.UPDATE,s,request);
            return R.ok();
        } catch (Exception e) {
            s = JSON.toJSONString(push);
            sysAuditService.saveFailAudit("行政处罚行为",Constant.PUNISH,Constant.PT,Constant.UPDATE,s,e,request);
            return R.error(e.getMessage());
        }
    }


    // 删除行政处罚行为信息表
    @PostMapping(value = "/deletePunish")
    public R deletePunish(@RequestParam("id") String id){
        String s;
        if(punishService.deletePunish(id)){
            s = JSON.toJSONString(id);
            sysAuditService.saveSuccessAudit("行政处罚行为", Constant.PUNISH,Constant.PT,Constant.DEL,s,request);
            return R.ok("操作成功!");
        }else{
            s = JSON.toJSONString(id);
            RuntimeException e = new RuntimeException("操作失败");
            sysAuditService.saveFailAudit("行政处罚行为",Constant.PUNISH,Constant.PT,Constant.DEL,s,e,request);
            return  R.error("操作失败!");
        }
    }

    /**
     *分页查询所有行政处罚行为信息表
     */
    @GetMapping(value = "/getAllPunish")
    public R getAllPunish(@RequestParam(required = false) Integer pageNo,
                         @RequestParam(required = false) Integer pageSize
    ) {
        String s="";
           try{
               PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
               QueryResults<Punish> punish = punishService.getAllPunish(pageRequest);
               s = JSON.toJSONString(punish);
               sysAuditService.saveSuccessAudit("行政处罚行为", Constant.PUNISH,Constant.PT,Constant.PAGE,s,request);
               return R.ok().put("data", punish);
           }catch (Exception e){
               e.printStackTrace();
               sysAuditService.saveFailAudit("行政处罚行为",Constant.PUNISH,Constant.PT,Constant.PAGE,s,e,request);
               return  R.error("操作失败!");
           }

    }

    /**
     * 多条件查询所有行政处罚行为信息表
     */
    @GetMapping(value ="/getAllConPunish")
    public R getAllConPunish(@RequestParam String recordUniqueIdentity,
                            @RequestParam(required = false) String superviseItemCode,
                            @RequestParam(required = false) String punishActionName,
                            @RequestParam(required = false) String punishActionCode,
                            @RequestParam(required = false) String implementInstitution,
                            @RequestParam(required = false) String implementInstitutionCode,
                            @RequestParam(required = false) Integer pageNo,
                            @RequestParam(required = false) Integer pageSize){
        String s="";
       try{
           PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
           QueryResults<Punish> allConPunish = punishService.getAllConPunish(pageRequest, recordUniqueIdentity, superviseItemCode, punishActionName, punishActionCode, implementInstitution, implementInstitutionCode);
           s = JSON.toJSONString(allConPunish);
           sysAuditService.saveSuccessAudit("行政处罚行为", Constant.PUNISH,Constant.PT,Constant.PAGE,s,request);
           return R.ok().put("data",allConPunish );
       }catch (Exception e){
           e.printStackTrace();
           sysAuditService.saveFailAudit("行政处罚行为",Constant.PUNISH,Constant.PT,Constant.PAGE,s,e,request);
           return  R.error("操作失败!");
       }


    }
    /***
     * 获取处罚行为
     * @return
     */
    @GetMapping("/punish")
    public R countPunish(){
        HashMap count=punishService.getCount();
        return R.ok().put("count",count);
    }
    @GetMapping("/punishyear")
    public R countPunishByYear(String year){
        HashMap count=punishService.getCountByYear(year);
        return R.ok().put("count",count);
    }
    /***
     * 获取当前登陆人的机构的处罚行为
     * @param request
     * @return
     */
    //@GetMapping("/getpunish")
    public R getOpPunish(HttpServletRequest request){
        Long count=punishService.getOpCheck(request);
        return R.ok().put("count",count);
    }

    /***
     * 多条件查询对外接口
     * @param superviseItemCode
     * @param punishActionName
     * @param caseSource
     * @param punishType
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping(value ="/getAll")
    public QueryResults<Punish> getAll(
                    @RequestParam(required = false) String superviseItemCode,
                    @RequestParam(required = false) String punishActionName,
                    @RequestParam(required = false) String caseSource,
                    @RequestParam(required = false) String punishType,
                    @RequestParam(required = false) Integer pageNo,
                    @RequestParam(required = false) Integer pageSize){

        PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
        return punishService.getAll(pageRequest,superviseItemCode,punishActionName,caseSource,punishType);
    }

    /***
     * 查询列表
     * @param superviseItemCode
     * @param punishActionName
     * @param caseSource
     * @param punishType
     * @return
     */
    @GetMapping(value ="/getAllList")
    public List<Punish> getAll(
            @RequestParam(required = false) String superviseItemCode,
            @RequestParam(required = false) String punishActionName,
            @RequestParam(required = false) String caseSource,
            @RequestParam(required = false) String punishType){
        List<Punish> list=punishService.getAllList(superviseItemCode,punishActionName,caseSource,punishType);
        return list;
    }

    /**
     * 获取总数
     * @return
     */
    @GetMapping("/countNum")
    public Long getCount(){
       Long num=punishService.getNum();
       return num;
    }

    /***
     * 根据ids查询
     * @param ids
     * @return
     */
    @GetMapping("/getByPuIds")
    public List<Punish> getByIds(@RequestParam String ids){
        List<String> arr= Arrays.asList(ids.split(","));
        List<Punish> list=punishService.getByIds(arr);
        return list;
    }
    //查询行政处罚行为信息表
    @GetMapping(value = "/getPunishById")
    public R getPunishById(@RequestParam("id") String id){
        return R.ok().put("data",punishService.getPunish(id));
    }


    /**
     * 查询行行政处罚行为所有信息
     * 对外提供接口
    *@Author weitao
    *@Date 9:45 2020/6/29
    */
    @GetMapping(value ="/getApList")
    public List<Punish> getApList(){

        try {
            List<Punish> list = punishService.getApList();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
